package net.pm.core.web.collarmaterial;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.Pager;
import net.pm.core.annotation.PowerCode;
import net.pm.core.annotation.PowerCode.PowerCodeEnum;
import net.pm.core.entities.db.Material;
import net.pm.core.service.CollarMaterialServicer;
import net.pm.core.service.MaterialServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
@RequestMapping("/collar/")
public class CollarmaterialController extends BaseController{
	
	@Resource(name="MaterialServicer")
	private MaterialServicer materialServicer;
	@Resource(name="CollarMaterialServicer")
	private CollarMaterialServicer collarServicer;
	/**
	 * 去到创建领料页面
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.COLLAR_MATERIAL)
	@RequestMapping(value="create",method=RequestMethod.GET)
	public ModelAndView creare(Long productId){
		return new ModelAndView("collar/collar_create").addObject("productId", productId);
	}
	
	/**
	 * 添加领料信息
	 * @param collarmap
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.COLLAR_MATERIAL)
	@RequestMapping(value="create",method=RequestMethod.POST)
	public ModelAndView create(String[] collarlogs,Long productId){
		//不知道,为什么封装map集合失败,,　　暂时采用String[] 来接收,再封装map(很不情愿这样做...)
		Map<Long,Integer> collarmap = new HashMap<Long,Integer>(collarlogs.length);
		for(String str : collarlogs){
			String[] ss = str.split("_");
			collarmap.put( Long.valueOf(ss[0]).longValue(),Integer.valueOf(ss[1]).intValue());
		}
		//---
		ModelAndView mav = new ModelAndView("redirect:/collar/result.htm");
		Long userId = super.obtainCurrentUser().getId();
		Long retval = collarServicer.createCollarAndLog(collarmap, productId, userId);
		if(retval.equals(CollarMaterialServicer.create_collarandlog_success)){
			super.setSessionMessage("领取物料成功!");
		}else{
			super.setSessionMessage("领取物料失败,请稍候重试!");
		}
		return mav;
	}
	
	/**
	 * 领取物料结果页面
	 * @return
	 */
	@RequestMapping(value="result",method=RequestMethod.GET)
	public String collarResult(){
		return "collar/collar_result";
	}
	
	/**
	 *  物料页面
	 * @return
	 */
	@RequestMapping(value="material/page",method=RequestMethod.GET)
	public ModelAndView page(){
		return this.page(DevConstant.PAGE_DEFAULT_SIZE,DevConstant.PAGE_DEFAULT_NO,null);
	}
	
	/**
	 *  物料页面
	 * @return
	 */
	@RequestMapping(value="material/page",method=RequestMethod.POST)
	public ModelAndView page(Integer pagesize,Integer pageno,String materialName){
		ModelAndView mav = new ModelAndView("collar/material_page");
		Pager<Material> pager = materialServicer.findMaterialPager(pagesize, pageno,materialName);
		mav.addObject("pager", pager);
		return mav;
	}
	
	
	
	
}
